package com.supreme.studyserver.component;

import com.supreme.commonboot.annotation.BaseProjectPath;
import com.supreme.commonboot.annotation.TempDataDir;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;

//@Component
public class QumCommandLineRunner implements CommandLineRunner {

    @Resource
    private ApplicationContext applicationContext;

    @BaseProjectPath
    private String baseProjectPath;

    @TempDataDir
    private String tempDataDir;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("QumCommandLineRunner: " + Arrays.toString(args));

        Path beanLoggerPath = Paths.get(baseProjectPath, tempDataDir, "beanLogger.log");
        File beanLoggerFile = Files.exists(beanLoggerPath) ? beanLoggerPath.toFile() : Files.createFile(beanLoggerPath).toFile();
        FileWriter fileWriter = new FileWriter(beanLoggerFile);

        for (String beanDefinitionName : applicationContext.getBeanDefinitionNames()) {
            fileWriter.write(beanDefinitionName + "\n");
        }

        fileWriter.close();
    }
}
